package com.ruoyi.web.controller.equipment;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.equipment.EquipmentFaultType;
import com.ruoyi.system.service.IEquipmentFaultTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 故障知识库Controller
 * 
 * @author ruoyi
 * @date 2023-04-19
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/equipment/knowledge" )
@Api(tags = "故障知识库Controller")
public class EquipmentFaultTypeController extends BaseController {

    private final IEquipmentFaultTypeService iEquipmentFaultTypeService;

    /**
     * 查询故障知识库列表
     */
    //@PreAuthorize("@ss.hasPermi('equipment:knowledge:list')")
    @ApiOperation(value = "查询故障知识库列表")
    @GetMapping("/list")
    public TableDataInfo list(EquipmentFaultType equipmentFaultType)
    {
        startPage();
        LambdaQueryWrapper<EquipmentFaultType> lqw = new LambdaQueryWrapper<EquipmentFaultType>();
        if (StringUtils.isNotBlank(equipmentFaultType.getFaultType())){
            lqw.eq(EquipmentFaultType::getFaultType ,equipmentFaultType.getFaultType());
        }
        if (StringUtils.isNotBlank(equipmentFaultType.getFaultInfo())){
            lqw.eq(EquipmentFaultType::getFaultInfo ,equipmentFaultType.getFaultInfo());
        }
        if (StringUtils.isNotBlank(equipmentFaultType.getFaultCode())){
            lqw.eq(EquipmentFaultType::getFaultCode ,equipmentFaultType.getFaultCode());
        }
        if (StringUtils.isNotBlank(equipmentFaultType.getEquipmentCode())){
            lqw.eq(EquipmentFaultType::getEquipmentCode ,equipmentFaultType.getEquipmentCode());
        }
        if (StringUtils.isNotBlank(equipmentFaultType.getEquipmentName())){
            lqw.like(EquipmentFaultType::getEquipmentName ,equipmentFaultType.getEquipmentName());
        }
        if (StringUtils.isNotBlank(equipmentFaultType.getEquipmentType())){
            lqw.eq(EquipmentFaultType::getEquipmentType ,equipmentFaultType.getEquipmentType());
        }
        if (StringUtils.isNotBlank(equipmentFaultType.getResolveMethod())){
            lqw.eq(EquipmentFaultType::getResolveMethod ,equipmentFaultType.getResolveMethod());
        }
        List<EquipmentFaultType> list = iEquipmentFaultTypeService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出故障知识库列表
     */
    //@PreAuthorize("@ss.hasPermi('equipment:knowledge:export')" )
    @ApiOperation(value = "导出故障知识库列表")
    @Log(title = "故障知识库" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(EquipmentFaultType equipmentFaultType) {
        LambdaQueryWrapper<EquipmentFaultType> lqw = new LambdaQueryWrapper<EquipmentFaultType>(equipmentFaultType);
        List<EquipmentFaultType> list = iEquipmentFaultTypeService.list(lqw);
        ExcelUtil<EquipmentFaultType> util = new ExcelUtil<EquipmentFaultType>(EquipmentFaultType. class);
        return util.exportExcel(list, "knowledge" );
    }

    /**
     * 获取故障知识库详细信息
     */
    //@PreAuthorize("@ss.hasPermi('equipment:knowledge:query')" )
    @ApiOperation(value = "获取故障知识库详细信息")
    @GetMapping(value = "/{faultTypeId}" )
    public AjaxResult getInfo(@PathVariable("faultTypeId" ) Long faultTypeId) {
        EquipmentFaultType equipmentFaultType=iEquipmentFaultTypeService.getByIdWithImg(faultTypeId);
        return AjaxResult.success(equipmentFaultType);
    }

    /**
     * 新增故障知识库
     */
    //@PreAuthorize("@ss.hasPermi('equipment:knowledge:add')" )
    @ApiOperation(value = "新增故障知识库")
    @Log(title = "故障知识库" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EquipmentFaultType equipmentFaultType) {
        int result=iEquipmentFaultTypeService.add(equipmentFaultType);
        return toAjax(result);
    }

    /**
     * 修改故障知识库
     */
    //@PreAuthorize("@ss.hasPermi('equipment:knowledge:edit')" )
    @ApiOperation(value = "修改故障知识库")
    @Log(title = "故障知识库" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EquipmentFaultType equipmentFaultType) {
        return toAjax(iEquipmentFaultTypeService.updateById(equipmentFaultType) ? 1 : 0);
    }

    /**
     * 删除故障知识库
     */
    //@PreAuthorize("@ss.hasPermi('equipment:knowledge:remove')" )
    @ApiOperation(value = "删除故障知识库")
    @Log(title = "故障知识库" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{faultTypeIds}" )
    public AjaxResult remove(@PathVariable Long[] faultTypeIds) {
        return toAjax(iEquipmentFaultTypeService.removeByIds(Arrays.asList(faultTypeIds)) ? 1 : 0);
    }
}
